/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.bean.JobsDutiesBean;
import com.koron.purpose.service.api.JobsDutiesService;
import com.koron.purpose.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "jobsDuties", description = "岗位职责")
@RestController
@RequestMapping("/jobsDuties")
public class JobsDutiesController {
    @Autowired
    private JobsDutiesService service;

    @ApiOperation("删除")
    @ApiImplicitParam(name = "id", value = "id",  paramType = "query", dataType = "String")
    @RequestMapping(value = "/delete.htm", method = {RequestMethod.POST})
    @Log(title = "删除公司岗位",businessModule = "岗位职责", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.delete(id);
    }

    @ApiOperation("详情")
    @ApiImplicitParam(name = "id", value = "id",  paramType = "query", dataType = "String")
    @RequestMapping(value = "/details.htm", method = {RequestMethod.GET})
    public MessageBean<?> details(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.details(id);
    }

    @ApiOperation("添加公司岗位")
    @RequestMapping(value = "/insert.htm", method = {RequestMethod.POST})
    @Log(title = "添加公司岗位",businessModule = "岗位职责", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> insert(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestBody JobsDutiesBean bean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.insert(bean, user);
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update.htm", method = {RequestMethod.POST})
    @Log(title = "修改公司岗位",businessModule = "岗位职责", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestBody JobsDutiesBean bean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.update(bean, user);
    }

    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/query.htm", method = {RequestMethod.GET})
    public MessageBean<?> query(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String name, String corpCode, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.list(name, corpCode, page, pageSize);
    }


    @ApiOperation("岗位下拉信息list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/querySelectDate.htm", method = {RequestMethod.GET})
    public MessageBean<?> querySelectDate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String name, String corpCode, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.querySelectDate(name, corpCode, page, pageSize);
    }


    @ApiOperation("定人定岗列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/positionList.htm", method = {RequestMethod.GET})
    public MessageBean<?> positionList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String name, String corpCode, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.positionList(name, corpCode, page, pageSize);
    }


    @ApiOperation("定人定岗矩阵")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isleader", value = "是否领导：0否1是",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/positionMatrix.htm", method = {RequestMethod.GET})
    public MessageBean<?> positionMatrix(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode, Integer isleader, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.positionMatrix(corpCode, isleader, page, pageSize);
    }


    @ApiOperation("危害因素矩阵")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/hazardMatrix.htm", method = {RequestMethod.GET})
    public MessageBean<?> hazardMatrix(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode, String name, Integer page, Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.hazardMatrix(corpCode, name, page, pageSize);
    }

    @ApiOperation("危害因素矩阵导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/exportHazardMatrix.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportHazardMatrix(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String corpCode, String name, Integer page, Integer pageSize) {
        if (null == user) {
            return null;
        }
        MessageBean<?> msg = service.hazardMatrix(corpCode, name, page, pageSize);
        if (msg.getCode() != 0) {
            return null;
        }
        if (msg.getData() == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", msg.getData());
        File targetFile = new File("static/excel/hazardMatrixExport.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/hazardMatrixExport.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            return null;
        }
        return Excel.createFile("危害因素矩阵", targetFile, "hazardMatrixExport.btl", map);
    }

    @ApiOperation("导入岗位职责模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "导入的文件",  paramType = "query", dataType = "String")})
    @RequestMapping(value = "/importExcel.htm", method = {RequestMethod.POST})
    public MessageBean<String> importExcel(StaffBean user, MultipartFile file){
        return service.importExcel(user, file);
    }


    @ApiOperation("下载导入岗位职责模板")
    @RequestMapping(value = "/exportTemplate.htm", method = RequestMethod.GET)
    public HttpEntity<?> exportTemplate(){
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/jobsDutiesTemplate.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/jobsDutiesTemplate.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            //logger.error(e.getMessage(),e);
        }
        return Excel.createFile("岗位职责模板", targetFile, "default.btl", map);
    }

}
